//
//  FullsreenImageView.m
//  99Gold
//
//  Created by Robin on 1/11/17.
//  Copyright © 2017 xia zhonglin . All rights reserved.
//

#import "FullsreenImageView.h"

@interface FullsreenImageView ()


@end

@implementation FullsreenImageView

- (instancetype)initWithFrame:(CGRect)frame ImageUrl:(NSString *)imageUrl {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColorFromRGB(0x000000);
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:Rect(0, 0, RectWidth(frame), RectHeight(frame))];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        NSURL * originUrl = [NSURL URLWithString:imageUrl];
        if (originUrl) {
            [imageView sd_setImageWithURL:originUrl placeholderImage:nil];
        }
        [self addSubview:imageView];
        
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissGesture:)];
        [imageView addGestureRecognizer:tapGestureRecognizer];
    }
    
    return self;
}

#pragma mark - public methods
- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    self.alpha = 0.f;
    self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
    [UIView animateWithDuration:0.2f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformMakeScale(1.05f, 1.05f);
        self.alpha = 1.f;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.08f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.transform = CGAffineTransformIdentity;
        } completion:nil];
    }];
}

#pragma mark - UITapGestureRecognizer
- (void)dismissGesture:(UITapGestureRecognizer *)sender {
    [UIView animateWithDuration:0.3f animations:^{
        self.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
        self.alpha = 0.f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
